<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" width="640px" top="0" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="80px">
			<el-row :gutter="16">
				<el-col :span="14">
					<el-form-item label="上级菜单" prop="pid">
						<sw-search-tree-select v-model="formData.pid" v-bind="treeSelectConfig" :tree-data="treeListData" :disabled="isDisabled" />
					</el-form-item>
				</el-col>
				<el-col :span="10">
					<el-form-item label="菜单类型" prop="level">
						<el-radio-group v-model="formData.level" v-removeAriaHidden:radio>
							<el-radio label="1">菜单</el-radio>
							<el-radio label="3">按钮</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item :label="`${labelName}名称`" prop="name">
						<el-input type="input" v-model="formData.name" maxlength="64" show-word-limit></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="formData.level === '1' ? 10 : 9">
					<el-form-item label="菜单图标">
						<select-icon v-model="formData.icon"></select-icon>
					</el-form-item>
				</el-col>
				<el-col :span="7" v-if="formData.level === '1'">
					<el-form-item :label="`是否业务${labelName}`" label-width="100px">
						<el-switch v-model="formData.isBusi" active-value="1" inactive-value="2" />
					</el-form-item>
				</el-col>
				<el-col :span="7" v-if="formData.level === '1'">
					<el-form-item :label="`是否开发${labelName}`" label-width="100px">
						<el-switch v-model="formData.isDev" active-value="1" inactive-value="2" />
					</el-form-item>
				</el-col>
				<el-col :span="formData.level === '1' ? 10 : 8">
					<el-form-item :label="`${labelName}排序`">
						<el-input-number v-model="formData.orderSort" controls-position="right" :min="1" style="width: 100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="7">
					<el-form-item :label="`是否激活${labelName}`" label-width="100px">
						<el-switch v-model="formData.del" active-value="1" inactive-value="2" />
					</el-form-item>
				</el-col>
				<el-col :span="7" v-if="formData.level === '1'">
					<el-form-item :label="`是否公共${labelName}`" label-width="100px">
						<el-switch v-model="formData.isPublic" active-value="1" inactive-value="2" />
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="formData.level === '1'">
					<el-form-item label="菜单路径" prop="url">
						<sw-custom-input type="address" v-model="formData.url" placeholder="请输入菜单路径" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item :label="`${labelName}描述`" prop="content">
						<el-input type="textarea" v-model="formData.content"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import lodash from 'lodash';
import { mapGetters } from 'vuex';
import selectIcon from './selectIcon';
import formMixins from '@/mixins/formMixins';
import { queryMenu, addedMenu, updateMenu } from '@/api/system/tbSysMenu';

export default {
	mixins: [formMixins],
	components: { selectIcon },
	props: {
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			formName: '菜单',
			formRules: {
				pid: [{ required: true, message: '请选择上级菜单', trigger: 'change' }],
				name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
				url: [{ required: true, message: '请输入菜单路径', trigger: 'change' }]
			},
			treeSelectConfig: {
				placeholder: '选择菜单',
				style: { width: '100%' },
				defaultProps: { label: 'name', children: 'children' }
			}
		};
	},
	computed: {
		...mapGetters(['userInfo']),
		detailData() {
			return eval(queryMenu);
		},
		callFunction() {
			return eval(!this.addOrEdit && !this.child ? updateMenu : addedMenu);
		},
		isDisabled() {
			return !!this.child;
		},
		treeListData() {
			let children = this.list;
			return [{ id: '0', name: '顶级菜单', children }];
		},
		labelName() {
			return this.formData.level === '3' ? '按钮' : '菜单';
		}
	},
	methods: {
		formatFormParams() {
			if (!this.addOrEdit && !this.child) return false;
			this.$set(this.formData, 'del', '1');
			this.$set(this.formData, 'level', '1');
			this.$set(this.formData, 'isDev', '1');
			this.$set(this.formData, 'isBusi', '1');
			this.$set(this.formData, 'isPublic', '1');
		},

		//获取选中表单数据
		getCheckedFormData(id) {
			let datas = this.$findNodeInTree(this.list, id);
			if (Object.keys(datas).length === 0) {
				this.$set(this.formData, 'level', '1');
			} else if (datas.pid === '0') {
				this.$set(this.formData, 'level', '2');
			} else {
				this.$set(this.formData, 'level', '3');
			}
		},

		updateStoreData() {
			this.$store.dispatch('getAuthMenu', this.userInfo.id);
		}
	}
};
</script>
<style lang="scss" scoped></style>
